<?php
/**
 * 抓取器
 * @author Yueool
 */
namespace common\libraries;

class Collector{

    /**
     * 获得原始文本
     * @param $url
     * @return bool|null|string|string[]
     */
    public static function getText($url){
        $text = file_get_contents($url);
        $text = preg_replace('/[\r\n]/', '', $text);//去换行
        $text = preg_replace('/\s{2,}/', ' ', $text);//多个空格换成一个空格
        return $text;
    }

    /**
     * 匹配出TITLE
     * @param $text
     * @return string
     */
    public static function getTitle($text){
        preg_match('/\<title\>(.*?)\<\/title\>/', $text, $matches);
        $title = isset($matches[1]) ? $matches[1] : "";
        $title = explode("-", $title)[0];
        return $title;
    }

    /**
     * 去掉注释
     * @param $content
     * @return null|string|string[]
     */
    public static function dislodgeAnnotation($content){
        $pattern = '/\<\!--.*?--\>/i';
        $content = preg_replace($pattern, '', $content);
        return $content;
    }
    
    
    /**
     * 支持HTTPS的CURL请求
     * @param $url
     * @param $params
     * @param int $timeout
     * @return mixed
     */
    public static function httpscurl($url, $params = [], $cookies = '', $timeout = 30){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//严格认证
        curl_setopt($ch, CURLOPT_HEADER, 0 ); // 过滤HTTP头
        $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);//如果有些页面返回403，把这个加上就好了
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果

        curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookies ); //读取 cookie
        curl_setopt($ch,CURLOPT_POST,true); // post传输数据
        curl_setopt($ch,CURLOPT_POSTFIELDS, $params);// post传输数据
        //curl_setopt ($ch, CURLOPT_COOKIE , $cookies );
        curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookies);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $responseText = curl_exec($ch);
        curl_close($ch);
        return $responseText;
    }

    /**
     * POST形式的file_get_contents 相当于POSTMAN 里的 BODY下x-www-form-urlencoded方式请求
     * 此方法对调用SERVLET效果非常好，都是UTF-8的时候不用转编码，而且不需要URLENCODE了
     * @param $url
     * @param $params
     * @return bool|string
     */
    public static function file_get_contents_post($url, $params){
        $context = stream_context_create([
            'http' => [
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => http_build_query($params),
                'timeout' => 20
            ]
        ]);
        return file_get_contents($url, false, $context);
    }


    /**
     * 保存网络文件到硬盘
     * @param $urlFile
     * @param $saveFile
     * @return bool
     */
    public static function saveUrlFile($urlFile, $saveFile){

        //参数判断
        if(empty($urlFile)){
            throw new \RuntimeException("URL文件参数不能为空");
            return false;
        }

        //资源判断
        $file = file_get_contents($urlFile);
        if(empty($file)){
            throw new \RuntimeException("得不到网络资源");
            return false;
        }

        //判断目录
        $saveDir = dirname($saveFile);
        if(empty($saveDir)){
            throw new \RuntimeException("无法从保存文件中获取保存目录");
            return false;
        }

        //创建保存目录
        if(!file_exists($saveDir)){
            if(!mkdir ($saveDir,0777,true)){
                throw new \RuntimeException("保存目录创建失败");
            }
        }

        //判断目录是否可写
        if(!is_writable($saveDir)){
            throw new \RuntimeException("目录不可写");
            return false;
        }

        //保存文件
        if(!file_put_contents($saveFile, $file)){
            throw new \RuntimeException("文件保存失败");
            return false;
        }

        return true;
    }

    /**
     * 随机字符串
     * @param $len
     * @return string
     */
    public static function randStr($len){
        $pattern = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $randkey = "";
        for($i = 0; $i< $len; $i++) {
            $randkey .= $pattern[mt_rand(0,35)];
        }
        return $randkey;
    }

}
